
// const shelljs = require('shelljs');
// (() => {
//     shelljs.echo('开始执行scss解析');
//     shelljs.exec(`sass --watch --style=compressed --no-source-map  scss:css`, async () => {});
//     // shelljs.exec(`live-server --port=6547`, async () => {});
// })();


var liveServer = require("live-server");
const chokidar = require('chokidar');
const sass = require('sass');
const path = require('path');
const fs = require('fs');


// const port = 3000;
// const app = express();
// app.use(express.static(__dirname));
// app.listen(port);

// console.log("服务已启动:\033[32m http://127.0.0.1:" + port + " \033[39m\n");
// open(`http://127.0.0.1:${port}`);

let timer = null;
//监听的文件
console.log('我已经开始自动解析scss文件');
chokidar.watch('**/*.scss', {
}).on('change', (file) => {
  timer && clearTimeout(timer);
  timer = setTimeout(() => {
    const { dir, name } = path.parse(file);
    const newDir = dir.replace(/^scss/, 'css');
    sass.render({
      file: file,
      outputStyle: "compressed"
    }, function (err, result) {
      if (!err) {
        const newFile = `${newDir}/${name}.css`;
        fs.writeFileSync(newFile, result.css);
        console.log(`updated ${newFile}`);
      }
    });
  }, 200);

});



//启动服务
// const port = 3000;
// liveServer.start({ port });



